﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wire;
using System.Reflection;
using System.Windows.Forms;
using System.Diagnostics;
using QuickTools.PluginUpdater;
using System.Threading;
using System.Drawing;

namespace QuickTools
{
    public abstract class PluginBase : Plugin
    {
        private readonly string title = "MatchmediaHelper";
        private readonly Version version;
        private GameInterface gameInterface;

        private ContextMenuStrip contextMenu = new ContextMenuStrip();
        private WirePluginUpdater updater = new WirePluginUpdater(Properties.Settings.Default.UpdateUrl, version);

        public PluginBase(string title, Version version)
        {
            this.title = title;
            this.version = version;
        }
        
        #region plugin interface

        public override void iconClicked(int x, int y, Plugin.MouseButton button)
        {
            switch (button)
            {
                case MouseButton.RightButton:
                    contextMenu.Show(x, y);
                    break;
                case MouseButton.LeftButton:
                    ShowSettings(null, EventArgs.Empty);
                    break;
            }
            base.iconClicked(x, y, button);
        }

        public override string Author
        {
            get { return "Lucas Romero"; }
        }

        public override string Title
        {
            get { return title; }
        }

        public override string Version
        {
            get
            {
                return version.ToString();
            }
        }

        // Init the plugin
        public override void init()
        {
            
        }

        #endregion

        #region Context menu
        private void initContextMenu(ContextMenuStrip menu)
        {
            menu.RenderMode = ToolStripRenderMode.ManagerRenderMode;
            menu.Font = new Font("Tahoma", 8);
            menu.ShowCheckMargin = false;
            menu.ShowItemToolTips = false;
            menu.AutoClose = true;

            /*
            menu.Items.Add(Resources.IgnoreWarnings, System.Resources.error, new EventHandler(IgnoreWarnings));
            menu.Items.Add(new ToolStripSeparator());
            menu.Items.Add(Resources.Settings, Resources.wrench, new EventHandler(ShowSettings));
            menu.Items.Add(Resources.CheckForUpdatesMenuItemTitle, Resources.box_search_result, new EventHandler(CheckForUpdate));
            menu.Items.Add(Resources.ReportBug, Resources.bug, new EventHandler(ReportBug));
            menu.Items.Add(Resources.About + " " + ShortTitle, Resources.information, new EventHandler(About));
            menu.Items.Add(new ToolStripSeparator());
            menu.Items.Add(Resources.CloseMenu, Resources.cancel);*/
        }

        private void ReportBug(object sender, EventArgs e)
        {
            try
            {
                Process.Start(@"http://code.google.com/p/quicktools/issues/list");
            }
            catch (Exception ex)
            {
               // MessageBox.Show(String.Format(Resources.StartBrowserError, ex.Message), "Error");
            }
        }

        private void CheckForUpdate(object sender, EventArgs e)
        {
            updater.CheckForUpdateAsync(true, true);
        }

        private void About(object sender, EventArgs e)
        {
            /*using (FormAbout f = new FormAbout())
            {
                f.ShowDialog();
            }*/
        }
        private void ShowSettings(object sender, EventArgs e)
        {
           /* using (FormOptions o = new FormOptions())
            {
                o.ShowDialog();
            }*/
        }

        #endregion

        private void updater_UpdateCheckComplete(object sender, UpdateCheckEventArgs e)
        {
            if (e.HasError)
            {
                if (e.Parameters.HandleErrors)
                    MessageBox.Show(String.Format(Resources.UpdateErrorText, e.Error.Message), Resources.UpdateErrorTitle);
                return;
            }
            lock (Settings.Default.AvailableUpdateChannels)
            {
                Settings.Default.AvailableUpdateChannels.Clear();
                foreach (UpdateChannel c in e.Updates.Channels)
                {
                    Settings.Default.AvailableUpdateChannels.Add(c.Name);
                }
                Settings.Default.Save();
            }
            try
            {
                WirePluginUpdater u = sender as WirePluginUpdater;
                Update update = u.GetPreferredUpdate(e.Updates);
#if DEBUG
                // if no upate is available and we are debugging,
                // just pretend the first update is the real deal
                update = update ?? e.Updates.Updates[0];
#endif
                if (update != null)
                {
                    Thread t = new Thread(new ParameterizedThreadStart(ShowUpdateWindow));
                    t.SetApartmentState(ApartmentState.STA);
                    t.Start(new ShowUpdateWindowParams { Updater = u, Updates = e.Updates, Update = update });
                    t.Join();
                }
                else if (e.Parameters.ShowMessageWhenNoUpdateAvailable)
                    MessageBox.Show(Resources.UpdateUpToDateText, Resources.UpdateUpToDateTitle);
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format(Resources.UpdateErrorText, ex.Message), Resources.UpdateErrorTitle);
            }
        }
        private class ShowUpdateWindowParams
        {
            public WirePluginUpdater Updater { get; set; }
            public UpdateCollection Updates { get; set; }
            public Update Update { get; set; }
        }

        private void ShowUpdateWindow(object state)
        {
            ShowUpdateWindowParams p = state as ShowUpdateWindowParams;
            FormUpdateAvailable f = new FormUpdateAvailable(p.Updater, p.Updates, p.Update);
            DialogResult choice = f.ShowDialog();
            /*DialogResult choice = MessageBox.Show(
                String.Format(Resources.UpdateAvailableText, update.Version, version, update.Channel),
                Resources.UpdateAvailableTitle,
                MessageBoxButtons.YesNo);*/
            if (choice == DialogResult.Yes)
            {
                p.Update.DownloadAsync();
                MessageBox.Show(Resources.UpdateInstallingText, Resources.UpdateInstallingTitle);
            }
        }
    }
}
